package B刷题.暴力递归.A回溯;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wei.zhao
 * @description: 47.全排列II
 * @date 2022年04月19日 11:20
 */
public class Code02_PermuteUnique {

    public List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> permuteUnique(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        boolean[] boos = new boolean[nums.length];
        Arrays.sort(nums);
        process(new ArrayList<>(), nums, boos);
        return result;
    }

    private void process(ArrayList<Integer> list, int[] nums, boolean[] boos) {
        if (list.size() == nums.length) {
            List<Integer> copyList = new ArrayList<>();
            for (Integer integer : list) {
                copyList.add(integer);
            }
            result.add(copyList);
        }
        for (int i = 0; i < nums.length; i++) {
            if (boos[i] || (i != 0 && nums[i - 1] == nums[i] && !boos[i - 1])) {
                continue;
            }
            list.add(nums[i]);
            boos[i]=true;
            process(list,nums,boos);
            list.remove(list.size()-1);
            boos[i]=false;
        }
    }

}
